package com.damai.plugins.impl;


import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

import com.damai.plugins.IPlugin;

public class ApacheHttpPlugin implements IPlugin {

	private static final ApacheHttpPlugin me = new ApacheHttpPlugin();
	private Log log = LogFactory.getLog(ApacheHttpPlugin.class);
	private List<CloseableHttpClient> list = new ArrayList<CloseableHttpClient>();
	
	public static final ApacheHttpPlugin me(){
		return me;
	}
	
	@Override
	public void start() {
		
	}

	@Override
	public void stop() {
		for (CloseableHttpClient closeableHttpClient : list) {
			try {
				closeableHttpClient.close();
			} catch (IOException e) {
				log.error(e);
			}
		}
	}
	
	public void register(CloseableHttpClient httpClient){
		list.add(httpClient);
	}
	
	public CloseableHttpClient create(){
		CloseableHttpClient client = HttpClients.createDefault();
		list.add(client);
		return client;
	}
}
